/*
 * 由 http://www.JSON.org/json2.js 得到
 */
String.prototype.jsonParse = function(){
	if (/^[\],:{}\s]*$/.test(this.replace(/\\["\\\/bfnrtu]/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, ''))){
		return eval('(' + this + ')');
	}
}

function encode(str){return window.encodeURIComponent ? encodeURIComponent(str) : escape(str);}
function decode(str){return window.decodeURIComponent ? decodeURIComponent(str) : unescape(str);}

/**
 * 添加一个元素
 * @param {Object} elm 元素类型
 * @param {Object} pObj 附着对象
 * @param {Object} id id
 * @param {Object} className 样式名
 */
function CreateElm(elm, pObj, id, className){
	var obj = null;
	obj = document.createElement(elm);
	if(! pObj){
		pObj = document.body;
	}
	if(id){
		obj.id = id;
	} 
	if(className){
		obj.className = className;
	}
	pObj.appendChild(obj);
	return obj;
}

/**
 * 根据ID返回元素对象
 * @param {Object} id
 */
function $(id){
	return document.getElementById(id);
}

function $gv( id){
	return $(id).value;
}

/**
 * 根据元素名返回对象
 * @param {Object} n
 */
function $n(n, flag){
	var objs = document.getElementsByName(n);
	return typeof(flag)=='undefined'?objs[0]:objs;
}

/**
 * 添加一个window对象加载完后执行的函数
 * @param {Object} fn 函数名
 */
function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
}

/**
 * 停止事件的默认动作
 * @param {Object} event
 */
function stopDefaultAction(event)
{
    event.returnValue = false;

    if (typeof event.preventDefault != "undefined")
    {
        event.preventDefault();
    }

    return true;
}

/**
 * 停止事件冒泡
 * @param {Object} event
 */
function stopEvent(event)
{
  if (typeof event.stopPropagation != "undefined")
  {
    event.stopPropagation();
  }
  else
  {
    event.cancelBubble = true;
  }

  return true;
}

/**
 * 获取元素的计算样式
 * @param {Object} element
 * @param {Object} styleProperty
 */
function retrieveComputedStyle(element, styleProperty)
{
  var computedStyle = null;

  if (typeof element.currentStyle != "undefined")
  {
    computedStyle = element.currentStyle;
  }
  else
  {
    computedStyle = document.defaultView.getComputedStyle(element, null);
  }

  return computedStyle[styleProperty];
}

/**
 * 获取光标位置
 * @param {Object} e
 */
function getCursorPosition(event)
{
  if (typeof event == "undefined")
  {
    event = window.event;
  }
  
  var cursorPosition = [0, 0];

  if (typeof event.pageX != "undefined" && typeof event.x != "undefined")
  {
    cursorPosition[0] = event.pageX;
    cursorPosition[1] = event.pageY;
  }
  else
  {
  	var scrollingPosition = getScrollingPosition();
    cursorPosition[0] = event.clientX + scrollingPosition[0];
    cursorPosition[1] = event.clientY + scrollingPosition[1];
  }
  return cursorPosition;
}

/**
 * 获取键盘代码
 * @param {Object} event
 */
function getKeyCode(e)
{
	return typeof(e)=='undefined' ? window.event.keyCode : e.keyCode;
}

/**
 * 得到元素对象的绝对坐标
 * @param {Object} theElement 元素对象
 */
function getPosition(theElement)
{
  var positionX = 0;
  var positionY = 0;
  while (theElement != null)
  {
    positionX += theElement.offsetLeft;
    positionY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }
  return [positionX, positionY];
}

/**
 * 获取IE窗口可视区大小
 */
function getViewportSize()
{
  var size = [0, 0];

  if (typeof window.innerWidth != 'undefined')
  {
    size = [
        window.innerWidth,
        window.innerHeight
    ];
  }
  else if (typeof document.documentElement != 'undefined'
      && typeof document.documentElement.clientWidth != 'undefined'
      && document.documentElement.clientWidth != 0)
  {
    size = [
        document.documentElement.clientWidth,
        document.documentElement.clientHeight
    ];
  }
  else
  {
    size = [
        document.getElementsByTagName('body')[0].clientWidth,
        document.getElementsByTagName('body')[0].clientHeight
    ];
  }

  return size;
}

/**
 * 获取窗口滚动条滚动的位置
 */
function getScrollingPosition()
{
  var position = [0, 0];

  if (typeof window.pageYOffset != 'undefined')
  {
    position = [
        window.pageXOffset,
        window.pageYOffset
    ];
  }

  else if (typeof document.documentElement.scrollTop != 'undefined'
      && (document.documentElement.scrollTop > 0 ||
      document.documentElement.scrollLeft > 0))
  {
    position = [
        document.documentElement.scrollLeft,
        document.documentElement.scrollTop
    ];
  }

  else if (typeof document.body.scrollTop != 'undefined')
  {
    position = [
        document.body.scrollLeft,
        document.body.scrollTop
    ];
  }
  return position;
}

/**
 * 获取事件的源元素
 */
function getEventTarget(event, nodeName)
{
  if (typeof event == "undefined")
  {
    event = window.event;
  }
  var targetElement = null;

  if (typeof event.target != "undefined")
  {
    targetElement = event.target;
  }
  else
  {
    targetElement = event.srcElement;
  }

  while (targetElement.nodeType == 3 && targetElement.parentNode != null)
  {
    targetElement = targetElement.parentNode;
  }
  //获取正确的元素
  if( nodeName)
  {
  	while( targetElement.nodeName.toLowerCase() != nodeName)
	{
		targetElement = targetElement.parentNode;
	}
  }

  return targetElement;
}

/**
 * 获取元素的绝对高度,css样式的height值
 * @param {Object} elm
 */
function getHeight( elm){
	var top = parseInt(retrieveComputedStyle(elm,'paddingTop'));
	var bottom = parseInt(retrieveComputedStyle(elm, 'paddingBottom'));
	return elm.clientHeight - top - bottom;
}
/**
 * 获取元素的绝对宽度
 * @param {Object} elm
 */
function getWidth( elm){
	var left = parseInt(retrieveComputedStyle(elm,'paddingLeft'));
	var right = parseInt(retrieveComputedStyle(elm,'paddingRight'));
	return elm.clientWidth - left - right;
}

/**
 * 获取页面真实大小
 */
function getPageDimensions()
{
  var body = document.getElementsByTagName("body")[0];
  var bodyOffsetWidth = 0;
  var bodyOffsetHeight = 0;
  var bodyScrollWidth = 0;
  var bodyScrollHeight = 0;
  var pageDimensions = [0, 0];

  if (typeof document.documentElement != "undefined" &&
      typeof document.documentElement.scrollWidth != "undefined")
  {
    pageDimensions[0] = document.documentElement.scrollWidth;
    pageDimensions[1] = document.documentElement.scrollHeight;
  }

  bodyOffsetWidth = body.offsetWidth;
  bodyOffsetHeight = body.offsetHeight;
  bodyScrollWidth = body.scrollWidth;
  bodyScrollHeight = body.scrollHeight;

  if (bodyOffsetWidth > pageDimensions[0])
  {
    pageDimensions[0] = bodyOffsetWidth;
  }

  if (bodyOffsetHeight > pageDimensions[1])
  {
    pageDimensions[1] = bodyOffsetHeight;
  }

  if (bodyScrollWidth > pageDimensions[0])
  {
    pageDimensions[0] = bodyScrollWidth;
  }

  if (bodyScrollHeight > pageDimensions[1])
  {
    pageDimensions[1] = bodyScrollHeight;
  }

  return pageDimensions;
}

/**
 * 获取事件的源元素对象
 * @param {Object} event
 */
function getEventTarget(event)
{
  var targetElement = null;

  if (typeof event.target != "undefined")
  {
    targetElement = event.target;
  }
  else
  {
    targetElement = event.srcElement;
  }

  while (targetElement.nodeType == 3 && targetElement.parentNode != null)
  {
    targetElement = targetElement.parentNode;
  }

  return targetElement;
}

/**
 * 添加事件监听
 * @param {Object} target 目标元素对象
 * @param {Object} eventType 监听类型
 * @param {Object} functionRef 触发后的函数
 * @param {Object} capture 是否冒泡
 */
function attachEventListener(target, eventType, functionRef, capture)
{
  if (typeof target.addEventListener != "undefined")
  {
    target.addEventListener(eventType, functionRef, capture);
  }
  else if (typeof target.attachEvent != "undefined")
  {
    target.attachEvent("on" + eventType, functionRef);
  }
  else
  {
    eventType = "on" + eventType;

    if (typeof target[eventType] == "function")
    {
      var oldListener = target[eventType];

      target[eventType] = function()
      {
        oldListener();

        return functionRef();
      }
    }
    else
    {
      target[eventType] = functionRef;
    }
  }

  return true; 
}

/**
 * 去除某个监听动作
 * @param {Object} target
 * @param {Object} eventType
 * @param {Object} functionRef
 * @param {Object} capture
 */
function detachEventListener(target, eventType, functionRef, capture)
{
  if (typeof target.removeEventListener != "undefined")
  {
    target.removeEventListener(eventType, functionRef, capture)
  }
  else if (typeof target.detachEvent != "undefined")
  {
    target.detachEvent("on" + eventType, functionRef);
  }
  else
  {
    target["on" + eventType] = null;
  }

  return true;
}

//去除空格
function trim(str){
	return str.replace(/\s/g,'');
}
//是否为空
function isEmpty(str){
	return trim(str)=='';
}
//是否是数字
function isInt(str){
	return /^\d{1,}$/.test(str);
}

//访问body
function getBody(){
	return document.body?document.body:document.getElementsByTagName('body')[0];
}

//添加/删除遮罩层
function controlSheet( display){
	if( display){
		var pageDimensions = getPageDimensions();
		var viewportSize = getViewportSize();
		if (viewportSize[1] > pageDimensions[1])
		{
			pageDimensions[1] = viewportSize[1];
		}
		var dropSheet = $("dropSheet");
		if( ! dropSheet){
			dropSheet = document.createElement("div");
			dropSheet.setAttribute("id", "dropSheet");
		}
		dropSheet.style.position = "absolute";
		dropSheet.style.left = "0";
		dropSheet.style.top = "0";
		dropSheet.style.width = pageDimensions[0] + "px";
		dropSheet.style.height = pageDimensions[1] + "px";
		getBody().appendChild(dropSheet);
	}else if($('dropSheet')){
		getBody().removeChild($('dropSheet'));
	}
	
}

//添加删除帧
function controlFrame( display, src){
	var div = $('divFrame');
	if( display){
		if( ! div){
			div = document.createElement('div');
			div.setAttribute("id", "divFrame");
		}
		div.style.position = 'absolute';
		var html = "<div id='divHead'><img src='"+baseUrl+"images/admin/close.gif' onclick='controlFrame(0);controlSheet(0);' /></div>";
		html += "<div><iframe src='"+src+"' frameborder='0'></iframe></div>";
		div.innerHTML = html;
		getBody().appendChild(div);
		attachEventListener(window, 'scroll', positionFrame, 0);
		attachEventListener(window, 'resize', positionFrame, 0);
		positionFrame();
	}else if( div){
		detachEventListener(window, 'scroll', positionFrame, 0)
		detachEventListener(window, 'resize', positionFrame, 0)
		getBody().removeChild($('divFrame'));
	}
}
function positionFrame(){
	var div = $('divFrame');
	if( div){
		var viewportSize = getViewportSize();
		var width = Math.max(viewportSize[0] - 100,600);
		var height = Math.max(viewportSize[1] -100,400);
		div.style.width = width + 'px';
		div.style.left = (viewportSize[0]-width)/2 + 'px';
		div.style.top = (viewportSize[1]-height)/4 + getScrollingPosition()[1] + 'px';
		var iframe = div.getElementsByTagName('iframe')[0];
		iframe.style.width = width + 'px';
		iframe.style.height = height + 'px';
	}
}

//图片上传前预览
function picPreview(inputId, previewId){ 
	var x = $( inputId); 
	if(!x || !x.value){ 
		return;
	} 
	var patn = /\.jpg$|\.jpeg$|\.gif|\.png|\.bmp$/i; 
	if(patn.test(x.value)){
		$(previewId).src = "file://localhost/" + x.value;
	}
}

